from wtforms import StringField, SubmitField, validators, IntegerField, SelectField, PasswordField, BooleanField
from flask_wtf import FlaskForm
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from src.model.models import *


# 注册form
class RegisterForm(FlaskForm):
    email = StringField('邮箱', validators=[validators.DataRequired(message='长度为8-60'),
                                          validators.Length(min=8, max=60), validators.Email(message='邮件格式有误')],
                        render_kw={'placeholder': 'yourname@example.com'})
    name = StringField('真实姓名', validators=[validators.DataRequired(message='长度为2-10'),
                                              validators.Length(min=2, max=10)])
    password = PasswordField('密码', validators=[validators.DataRequired(message='请输入密码')])

    submit = SubmitField('立即注册')


# 登陆form
class LoginForm(FlaskForm):
    email = StringField('邮箱', validators=[validators.DataRequired(message='长度为8-60'),
                                          validators.Length(min=8, max=60), validators.Email(message='邮件格式有误')],
                        render_kw={'placeholder': 'yourname@example.com'})
    password = PasswordField('密码', validators=[validators.DataRequired(message='请输入密码')])
    remember_me = BooleanField('记住我')

    submit = SubmitField('登陆')


class ChoiseProduct(FlaskForm):
    product = DimProduct.query.filter_by(is_valid=1).all
    product_name = QuerySelectField(query_factory=product, get_pk=lambda a: a.id, get_label=lambda a: a.product_name,
                                    label='产品名称')
    submit = SubmitField('提交')


class Product(FlaskForm):
    product_name = StringField('产品名称', validators=[validators.DataRequired(message='长度为2-25'),
                                                   validators.Length(min=2, max=25)])
    product_describe = StringField('产品描述')
    submit = SubmitField('提交')


class DbDriverType(FlaskForm):
    db_driver_name = StringField('数据库驱动类型', validators=[validators.DataRequired(message='长度为2-15'),
                                                        validators.Length(min=2, max=15)])
    db_driver_describe = StringField('数据库驱动类型描述')
    submit = SubmitField('提交')


class DataType(FlaskForm):
    db_driver = DimDbDriverType.query.filter_by(is_valid=1).all
    data_type_name = StringField('数据类型', validators=[validators.DataRequired(message='长度为2-15'),
                                                     validators.Length(min=2, max=15)])
    data_type_describe = StringField('数据类型描述')
    db_driver_id = QuerySelectField(query_factory=db_driver,
                                    get_pk=lambda a: a.id, get_label=lambda a: a.db_driver_name,
                                    label='数据库驱动类型')
    submit = SubmitField('提交')


class DbbDriverRelate(FlaskForm):
    db_driver = DimDbDriverType.query.filter_by(is_valid=1).all

    db_name = StringField('数据库名称', validators=[validators.DataRequired(message='长度为3-50'),
                                               validators.Length(min=3, max=20)])
    db_describe = StringField('数据库描述')

    db_type = QuerySelectField(query_factory=db_driver, get_pk=lambda a: a.id, get_label=lambda a: a.db_driver_name,
                               label='数据库驱动类型')
    submit = SubmitField('提交')


class DbModify(FlaskForm):
    db_code = StringField('数据库名', validators=[validators.DataRequired(message='长度为3-50'),
                                              validators.Length(min=3, max=20)])
    db_describe = StringField('数据库描述')

    submit = SubmitField('提交')


class TableColumn(FlaskForm):
    data_type = DimDataType.query.filter_by(is_valid=1).all
    column_name = StringField('属性名称', validators=[validators.DataRequired(message='长度为2-20'),
                                                  validators.Length(min=2, max=20)])

    data_type_id = QuerySelectField(query_factory=data_type, get_pk=lambda a: a.id,
                                    get_label=lambda a: a.data_type_name, label='属性数据类型')
    data_type_length = IntegerField('属性类型长度', validators=[validators.Optional()])
    column_describe = StringField('属性描述')

    submit = SubmitField('提交')


class TableColumnChoice(FlaskForm):
    column_name_data = DimColumn.query.filter_by(is_valid=1).all

    column_name = QuerySelectField(query_factory=column_name_data, get_pk=lambda a: a.id,
                                   get_label=lambda a: a.column_name, label='请选择属性')

    submit = SubmitField('提交')


class DbChoice(FlaskForm):
    db_choice = DimDb.query.filter_by(is_valid=1).all
    db_name = QuerySelectField(query_factory=db_choice, get_pk=lambda a: a.id,
                               get_label=lambda a: a.db_name, label='请选择数据库')
    submit = SubmitField('提交')


class ColumnCategory(FlaskForm):
    category_name = StringField('分类名称', validators=[validators.DataRequired(message='长度为2-20'),
                                                    validators.Length(min=2, max=20)])
    submit = SubmitField('提交')


class StandColumn(FlaskForm):
    data_type = DimDataType.query.filter_by(is_valid=1).all
    category = DimColumnCategory.query.filter_by(is_valid=1).all

    column_name = StringField('属性名称', validators=[validators.DataRequired(message='长度为2-20'),
                                                  validators.Length(min=2, max=20)])

    data_type_id = QuerySelectField(query_factory=data_type, get_pk=lambda a: a.id,
                                    get_label=lambda a: a.data_type_name, label='属性数据类型')
    data_type_length = IntegerField('属性类型长度', validators=[validators.Optional()])
    column_describe = StringField('属性描述')
    category_id = QuerySelectField(query_factory=category, get_pk=lambda a: a.id,
                                   get_label=lambda a: a.category_name, label='属性分类')
    type_id = SelectField('维度/度量分类', choices=[(1, '维度'), (2, '度量')], coerce=int)

    is_null = SelectField('是否可空', choices=[(True, '可空'), (False, '不可空')], coerce=bool)
    is_key = SelectField('是否主键', choices=[(False, '非主键'), (True, '主键')], coerce=bool)
    defalut_value = StringField('默认值')

    submit = SubmitField('提交')


# hive table参数
class HiveTableParameter(FlaskForm):
    table_type = SelectField('请选择表类型', validators=[validators.DataRequired],
                             choices=[(1, '管理表'), (2, '外部表')])

    partition_column = StringField('请填写分区字段，用逗号分开', validators=[validators.Optional])

    cluster_column = StringField('请填写分桶簇字段', validators=[validators.Optional])
    sort_column = StringField('请填写分桶排序字段', validators=[validators.Optional])
    nums = IntegerField('请填写分桶数量', validators=[validators.NumberRange(0, 50)])

    line_terimial = StringField('请填写分行识别符', validators=[validators.DataRequired])
    fields_terimial = StringField('请填写分字段识别符', validators=[validators.DataRequired])
    formatdata = SelectField('请选择存储格式', validators=[validators.DataRequired],
                             choices=[(1, 'TEXTFILE'), (2, 'ORC'), (3, 'RCFILE'), (4, 'SEQUENCEFILE'),
                                      (5, 'PARQUET'), (6, 'AVRO'), (7, 'JSONFILE')])
    save_path = StringField('请填写存储位置', validators=[validators.Optional])
    submit = SubmitField('提交')


# 用户
class UserForm(FlaskForm):
    role = Role.query.filter_by(is_valid=1).all

    email = StringField('邮箱', validators=[validators.DataRequired(message='长度为8-60'),
                                          validators.Length(min=8, max=60), validators.Email(message='邮件格式有误')],
                        render_kw={'placeholder': 'yourname@example.com'})
    name = StringField('真实姓名', validators=[validators.DataRequired(message='长度为2-10'),
                                              validators.Length(min=2, max=10)])
    role_id = QuerySelectField(query_factory=role, get_pk=lambda a: a.id, get_label=lambda a: a.name, label='角色')

    submit = SubmitField('提交')


# 修改个人信息
class PersonModifyForm(FlaskForm):
    email = StringField('邮箱', validators=[validators.Length(min=8, max=60),
                                          validators.Email(message='邮件格式有误')],
                        render_kw={'placeholder': 'yourname@example.com'})
    password = PasswordField('密码')

    submit = SubmitField('确认修改')
